﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using exsportance.Silverlight.Common.Cryptography;

namespace exsportance.Silverlight.Common.Utils
{
    public static class StringUtils
    {
        /// <summary>
        /// format the string with given arguments
        /// </summary>
        /// <param name="value"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string Frmt(this string value, params object[] args)
        {
            return String.Format(value, args);
        }

        /// <summary>
        /// Indicates whether the specified System.String object is null or an System.String.Empty string.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string value)
        {
            return String.IsNullOrEmpty(value);
        }

        /// <summary>
        /// Adds content to a string. If string is not empty a separator will be added
        /// </summary>
        /// <param name="value"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Add(this string value, string content)
        {
            return Add(value, content, ",");
        }

        /// <summary>
        /// Adds content to a string. If string is not empty a separator will be added
        /// </summary>
        /// <param name="value"></param>
        /// <param name="content"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string Add(this string value, string content, string separator)
        {
            string result = value;

            if (!result.IsNullOrEmpty() && !content.IsNullOrEmpty())
                result += separator;

            result += content;

            return result;
        }

        /// <summary>
        /// Converts string to absolute uri
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Uri ToUri(this string value)
        {
            return UriUtils.GetUri(value);
        }

        /// <summary>
        /// Checks by RegEx if value is valid emmail
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsEmail(this string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
             @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
             @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

                // string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" +
                //@"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" +
                //@"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";


                Match match = Regex.Match(value, strRegex, RegexOptions.IgnoreCase);
                return match.Success;
            }
            else
                return false;
        }

        /// <summary>
        /// Checks by RegEx if value is valid url
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsUrl(this string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                string strRegex = "^(https?://)"
       + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //user@ 
       + @"(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP- 199.194.52.184 
       + "|" // allows either IP or domain 
       + @"([0-9a-z_!~*'()-]+\.)*" // tertiary domain(s)- www. 
       + @"([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // second level domain 
       + "[a-z]{2,6})" // first level domain- .com or .museum 
       + "(:[0-9]{1,4})?" // port number- :80 
       + "((/?)|" // a slash isn't required if there is no file name 
       + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";

                Match match = Regex.Match(value, strRegex, RegexOptions.IgnoreCase);
                return match.Success;
            }
            else
                return false;
        }

        /// <summary>
        /// Get a MD5 hash
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetMd5(this string value)
        {
            return MD5Core.GetHashString(value);
        }

    }
}
